import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class MobilePong extends MIDlet implements CommandListener {
	
    private Display display;

    public MainGame theGame;

    public WelcomeScreenLv2 welcomeScreen = null;

    private MainScreenLv2 mainGameLv2;

    private boolean gameExit;


    public boolean started = false;

    static public ConnectionManager connectMan;

    private inGameMenu gameMenu;

    private Gauge indicator ;
    Alert starting;

    public void updateIndicator(String msg, boolean isIncrease){
		if (isIncrease) {
			indicator.setValue(indicator.getValue()+1);
        }
		if (msg != null) {
			starting.setString(msg);
		}
    }
    public void startApp() {
        Image[] imageArray = null;
        try {
                Image icon = Image.createImage("/Icon.png");
                imageArray = new Image[] { icon, icon, icon };
        } catch (java.io.IOException err) {
                // ignore image loading failure so application can recover.
        }
        this.display = Display.getDisplay(this);
        splashScreen canvas = new splashScreen(this);
        this.display.setCurrent(canvas);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void startMenu() {
        System.gc();
        this.display = Display.getDisplay(this);
        welcomeScreen = new WelcomeScreenLv2(this);
        display.setCurrent(welcomeScreen);
    }

    public void startSingleGame() {
        welcomeScreen = null;
        gameExit = false;
        startGame(MainGame.SINGLE);
    }
    private void initAlert(String title,String text){
        starting = new Alert(title,text,null,null);
        starting.setTimeout(-2);
        indicator=new Gauge(null, false, 5, 0);
        starting.setCommandListener(this);
        starting.addCommand(new Command("Cancel",Command.CANCEL,1));
        starting.setIndicator(indicator);
    }

    public void hostMultiplayerGame() {
        initAlert("Hosting","Hosted Game, Waiting for Clients.");
        this.display.setCurrent(starting);
        welcomeScreen = null;
        connectMan = new ConnectionManager(this, MainGame.HOST);
    }

    public void joinMultiplayerGame() {
        initAlert("Joining","Looking for Hosted Games.");
        this.display.setCurrent(starting);
        welcomeScreen = null;
        connectMan = new ConnectionManager(this, MainGame.JOIN);
    }

    public void startSpecialMode() {
		initAlert("Loading","Loading resources...");
        this.display.setCurrent(starting);
        welcomeScreen = null;
        System.gc();
        mainGameLv2 = new MainScreenLv2(this);
        //display.setCurrent(mainGameLv2);
    }

    public void exitGame() {
        destroyApp(false);
        notifyDestroyed();
    }

    void rdyToStart(int mode) {
        connectMan.start();
        startGame(mode);
    }

    public void stopGame() {
        MainGame.stop();
        if (MainGame.mode != MainGame.SINGLE)
                connectMan.reset();
        indicator = new Gauge(null, false, 5, 0);
    }

    public void startGame(int mode) {
        theGame = new MainGame(this, mode);
        if (mode == MainGame.HOST)
                connectMan.sendGameStart();
        while (mode == MainGame.JOIN && started == false);
        gameMenu = new inGameMenu(this);
        this.display.setCurrent(theGame);
        gameMenu.addCommands(theGame);
        theGame.start();
        System.out.println("Game Starting, Mode = " + mode);
    }

    public void exitLv2() {
        mainGameLv2 = null;
        System.gc();
        this.display = Display.getDisplay(this);
        welcomeScreen = new WelcomeScreenLv2(this);
        display.setCurrent(welcomeScreen);
    }
    //TODO need to figure out a way to actually cut the bluetooth connection. Easy way is to check a flag during the process..
    public void commandAction(Command c, Displayable d) {
        connectMan.reset();
        connectMan=null;
        startMenu();
    }

    void connectFailed() {
        starting.setTitle("Connection Failed");
        starting.setString("Bluetooth Connection Failed");
        commandAction(null,null);
    }
}
